package com.leyou.page.service;

import com.leyou.item.pojo.*;
import com.leyou.page.client.GoodsClient;
import com.leyou.page.client.SpecClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecClient specClient;

    public Map<String, Object> loadData(Long spuId) {
        Map<String, Object> result = new HashMap<>();
        //根据spu的id查询spu
        Spu spu = goodsClient.querySpuById(spuId);
        result.put("spu", spu);
        //根据spuId查询spuDetail
        SpuDetail spuDetail = goodsClient.querySpuDetailBySpuId(spuId);
        result.put("spuDetail", spuDetail);
        //根据spuId查询skus
        List<Sku> skus = goodsClient.querySkuBySpuId(spuId);
        result.put("skus", skus);
        // 根据分类查询特有的规格参数，并且处理成map 解构key为规格参数的id，值为规格参数的名字
        List<SpecParam> specailSpecParams = specClient.querySpecParams(null, spu.getCid3(), null, false);
        //存放对照表
        Map<Long, String> specParams = new HashMap<>();
        specailSpecParams.forEach(specailSpecParam -> {
            specParams.put(specailSpecParam.getId(), specailSpecParam.getName());
        });
        result.put("specParams", specParams);
        //需要根据分类查询规格组，同时查询组内的参数
        List<SpecGroup> specGroups = specClient.querySpecGroups(spu.getCid3());
        result.put("specGroups", specGroups);
        return result;
    }

}
